#include "ColorPlane.h"

ColorPlane::ColorPlane()
{
	Plane = NULL;
}
ColorPlane::ColorPlane(int H, int W, int Deep)
{
	create(H,W,Deep);
}
ColorPlane::ColorPlane(ColorPlane& a)
{
	create(a.H,a.W,a.Deep);
	operator=(a);
}
void ColorPlane::create(int H, int W, int Deep = 24)
{
	Plane = new double[H*W];
	this->Deep = Deep;
	this->H = H;
	this->W = W;
	H_in = H;
	W_in = W;

}
ColorPlane& ColorPlane::operator = (ColorPlane* Second)
{
	if (Plane == NULL)
		Plane = new double[Second->GetSize()];
	else
	{
		delete Plane;
		create(Second->GetH(),Second->GetW());
	}
	for (int i = 0; i < H; i++)
		for (int j = 0; j < W; j++)
		{
			double temp = Second->
				GetPixel(i,j);
			* (Plane + i*W + j) = temp;
		}
	Deep = Second->Deep;
	H_in = H;
	W_in = W;
	return *this;
}


ColorPlane* ColorPlane::operator + (ColorPlane* Second)
{
	ColorPlane* Sum;
	Sum = new ColorPlane;
	
	double temp;
	int H_ = 0;
	int W_ = 0;

			if (H >  Second->H)
			H_ = Second->H;
			else H_ = H;
			if (W >  Second->W)
			W_ = Second->W;
			else W_ = W;
	Sum->create(H_,W_,Deep);
	for (int i = 0; i < H_; i++)
		for (int j = 0; j < W_; j++)
		{	
 			temp = (* (Plane + i*W + j) + Second->GetPixel(i,j));
			Sum->SetPixel(i,j, temp);
		}

	return Sum;

}
ColorPlane* ColorPlane::operator - (ColorPlane* Second)
{
	ColorPlane* dif;
	dif = new ColorPlane;
	dif->create(H,W,Deep);
	double temp;
	for (int i = 0; i < H; i++)
		for (int j = 0; i < W; i++)
		{
			temp = (* (Plane + i*W + j) - Second->GetPixel(i,j));
			if (temp < 0) 
			dif->SetPixel(i,j,0);
			else dif->SetPixel(i,j,temp);
		}

		return dif;

}
ColorPlane* ColorPlane::operator * (ColorPlane* Second)
{
	ColorPlane* Mul;
	Mul = new ColorPlane;

	double temp;
	int H_ = 0;
	int W_ = 0;

			if (H >  Second->H)
			H_ = Second->H;
			else H_ = H;
			if (W >  Second->W)
			W_ = Second->W;
			else W_ = W;
	Mul->create(H_,W_,Deep);
	for (int i = 0; i < H_; i++)
		for (int j = 0; j < W_; j++)
		{	
			temp = (* (Plane + i*W + j) * Second->GetPixel(i,j));
			Mul->SetPixel(i,j, temp);
		}

		return Mul;

}
void ColorPlane::mult(double number)
{

	double temp;
	for (int i = 0; i < H; i++)
		for (int j = 0; j < W; j++)
		{	
			temp = *(Plane + i*W + j);
			temp *= number;
			*(Plane + i*W + j) = temp;
		}
}

void ColorPlane::summ(double number)
{
	double temp;
	for (int i = 0; i < H; i++)
		for (int j = 0; j < W; j++)
		{	
			temp = *(Plane + i*W + j);
			temp += number;
			*(Plane + i*W + j) = temp;
		}
}
void ColorPlane::SetArea(int H,int W)
{
	H_in = H;
	W_in = W;
}

double ColorPlane::GetPixel(int x,int y)
{
	return *(Plane + x*W + y);
}
double ColorPlane::GetPixel(int i)
{
	return *(Plane + i);
}


double * ColorPlane::GetPlaneAdress()
{
	return Plane;
}
void ColorPlane::SetPixel(int x, int y, double temp)
{
	*(Plane + x*W + y) = temp;
}
void ColorPlane::SetPixel(int i , double temp)
{
	*(Plane + i) = temp;
}


int ColorPlane::GetSize()
{
	return H*W;
}
int ColorPlane::GetW()
{
	return W;
}


int ColorPlane::GetH()
{
	return H;
}
double ColorPlane::GetAvr()
{
	double sum = 0;
	int size = H*W;
	for(int i = 0; i <size; i++ )
	{
		sum+=Plane[i];
	}
	return sum/size;
}
void ColorPlane::SetToConst(double a)
{
	int size = H*W;
	for (int i =0; i< size; i++)
	{
		Plane[i] = a;
	}
}
void ColorPlane::MakeCurves()
{
	for (int i = 0; i<H*W; i++)
	{
		if (Plane[i] != Plane[i+1])
		{
			Plane[i] = 1;
		} else
			Plane[i] = 0.000000;
	}
}
void ColorPlane::Make1Pix(double treshold)
{
	double* newPlane = new double[GetSize()];
	int size = GetSize();
	for (int i = W; i<size-1-W; ++i)
	{
		if (!Plane[i]&&fabs(Plane[i]-Plane[i-1])>treshold)
		{
			newPlane[i] = 0.5;


		}
		else if (!Plane[i]&&fabs(Plane[i]-Plane[i+1])>treshold)
		{
			newPlane[i] = 0.5;

		}
		else if (!Plane[i]&&fabs(Plane[i]-Plane[i+1+W])>treshold)
		{
			newPlane[i] = 0.5;

		}
		else if (!Plane[i]&&fabs(Plane[i]-Plane[i+W])>treshold)
		{
			newPlane[i] = 0.5;

		}
		else if (!Plane[i]&&fabs(Plane[i]-Plane[i-1+W])>treshold)
		{
			newPlane[i] = 0.5;

		}
		else if (!Plane[i]&&fabs(Plane[i]-Plane[i+1-W])>treshold)
		{
			newPlane[i] = 0.5;

		}
		else if (!Plane[i]&&fabs(Plane[i]-Plane[i-W])>treshold)
		{
			newPlane[i] = 0.5;

		}
		else if (!Plane[i]&&fabs(Plane[i]-Plane[i-1-W])>treshold)
		{
			newPlane[i] = 0.5;

		}else
		{
			newPlane[i] = 0;
		}
	}
	delete Plane;
	Plane = newPlane;
}
double ColorPlane::AvaerageBr()
{
	double summ = 0;
	for (int j = 0; j < W*H; j++)
		summ += *(Plane + j);
	summ /= W*H;
	return summ;
}
double ColorPlane::findClosest(double temp)
{
	for (int i = 0; i < W*H; i++)
	{
		if (fabs(Plane[i] - temp)<= 0.001)
		return Plane[i];
	}
}
void ColorPlane::Potent()
{
  	for (int i = 0 ; i < H * W; i ++)
	{
		double temp = 0;
		temp=Plane[i];
		Plane[i] = pow(2.718281828,temp);
		Plane[i] /= 4;
	}
}
ColorPlane::~ColorPlane()
{
	delete [] Plane;
}

